package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.domain.imp.SpecImport;
import com.ruoyi.system.domain.imp.TypeImport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Spec;
import com.ruoyi.system.service.ISpecService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 竞品规格Controller
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@RestController
@RequestMapping("/system/spec")
public class SpecController extends BaseController {
    @Resource
    private ISpecService specService;

    /**
     * 查询竞品规格列表
     */
    //@PreAuthorize("@ss.hasPermi('system:spec:list')")
    @GetMapping("/list")
    public TableDataInfo list(Spec spec) {
        if (Convert.toInt(ServletUtils.getParameter("pageNum")) != -1
                && Convert.toInt(ServletUtils.getParameter("pageSize")) != -1) {
            startPage();
        }
        List<Spec> list = specService.selectSpecList(spec);
        return getDataTable(list);
    }

    /**
     * 导出竞品规格列表
     */
    //@PreAuthorize("@ss.hasPermi('system:spec:export')")
    @Log(title = "竞品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spec spec) {
        List<Spec> list = specService.selectSpecList(spec);
        ExcelUtil<Spec> util = new ExcelUtil<Spec>(Spec.class);
        util.exportExcel(response, list, "竞品规格数据");
    }

    /**
     * 获取竞品规格详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:spec:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(specService.selectSpecById(id));
    }

    /**
     * 新增竞品规格
     */
    //@PreAuthorize("@ss.hasPermi('system:spec:add')")
    @Log(title = "竞品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Spec spec) {
        return toAjax(specService.insertSpec(spec));
    }

    /**
     * 修改竞品规格
     */
    //@PreAuthorize("@ss.hasPermi('system:spec:edit')")
    @Log(title = "竞品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Spec spec) {
        return toAjax(specService.updateSpec(spec));
    }

    /**
     * 删除竞品规格
     */
    //@PreAuthorize("@ss.hasPermi('system:spec:remove')")
    @Log(title = "竞品规格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(specService.deleteSpecByIds(ids));
    }

    //@PreAuthorize("@ss.hasPermi('system:spec:import')")
    @Log(title = "规格管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SpecImport> excelUtil = new ExcelUtil<>(SpecImport.class);
        List<SpecImport> list = excelUtil.importExcel(file.getInputStream());
        specService.importSpec(list);
        return AjaxResult.success();
    }
}
